package ru.elifantiev.cityrouter.infrastructure.map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import ru.elifantiev.cityrouter.GeoSearchActivity;
import ru.elifantiev.cityrouter.R;

public class MapControlsWrapper {

    private Activity home;
    private boolean mapMode = MapWrapper.MAP_MODE_VECTOR;
    private boolean isLocationOn = false;
    private Button btnEnablePositioning, btnMapMode;
    private MapWrapper map;

    private final String STATE_KEY_MAP_MODE = "ru.hippocamp.MAP_MODE";
    private final String STATE_KEY_LOCATION_MODE = "ru.hippocamp.MYLOCATION";

    public static final int REQUEST_SELECT_ADDRESS = 2;

    public MapControlsWrapper(Activity homeActivity, final MapWrapper map) {

        this.home = homeActivity;
        this.map = map;

        if (map instanceof ExternalZoomable) {
            home.findViewById(R.id.zoomControls).setVisibility(View.VISIBLE);
            home.findViewById(R.id.zoomDown).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ((ExternalZoomable) map).zoomOut();
                }
            });
            home.findViewById(R.id.zoomUp).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ((ExternalZoomable) map).zoomIn();
                }
            });
        } else
            home.findViewById(R.id.zoomControls).setVisibility(View.GONE);


        home.findViewById(R.id.btnDoSearch).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                doSearchQuery(((EditText) home.findViewById(R.id.edtSearchQuery)).getText().toString());
            }
        });

        /*btnMapMode = (Button) home.findViewById(R.id.btnMapMode);
        btnMapMode.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setMapModeState((Button) view, mapMode = !mapMode);
            }
        });*/

       /* home.findViewById(R.id.btnWhereAmI).setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                map.gotoMyLocation();
            }
        });*/


        /*btnEnablePositioning = (Button) home.findViewById(R.id.btnMyLocation);
        btnEnablePositioning.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setMyLocationState(view, isLocationOn = !isLocationOn);
            }
        });*/

        ((EditText) home.findViewById(R.id.edtSearchQuery)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_NULL) {
                    doSearchQuery(textView.getText().toString());
                }
                return false;
            }
        });
    }

    private void setMapModeState(Button indicator, boolean mapMode) {
        /*map.setMode(mapMode);
        indicator.setCompoundDrawablesWithIntrinsicBounds(
                mapMode ? R.drawable.ic_map_sat : R.drawable.ic_map_vector, 0, 0, 0);*/
    }

    private void setMyLocationState(View view, boolean state) {
        if (state)
            view.getBackground().setColorFilter(0xFF90E018, PorterDuff.Mode.MULTIPLY);
        else
            view.getBackground().setColorFilter(null);
        map.showMyLocation(state);
    }

    public void saveState(Bundle state) {
        state.putBoolean(STATE_KEY_MAP_MODE, mapMode);
        state.putBoolean(STATE_KEY_LOCATION_MODE, isLocationOn);
    }

    public void resume() {
    }

    public void pause() {
    }

    public void destroy() {
        home = null;
    }

    private void doSearchQuery(String queryText) {
        if (!queryText.equals("")) {
            Intent callToSearch = new Intent(home, GeoSearchActivity.class);
            callToSearch.putExtra(GeoSearchActivity.SEARCH_QUERY_EXTRA, queryText);
            home.startActivityForResult(callToSearch, REQUEST_SELECT_ADDRESS);
        }
    }

    public GeoPoint showSelectedLocation(Bundle pickedResult) {
        double lat = pickedResult.getDouble(GeoSearchActivity.SEARCH_RESULT_LATITUDE, -1);
        double lon = pickedResult.getDouble(GeoSearchActivity.SEARCH_RESULT_LONGITUDE, -1);
        if (lat != -1 && lon != -1) {
            GeoPoint retval = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
            map.showFoundLocationOverlay(retval);
            return retval;
        }
        return null;
    }

    public void restoreState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            isLocationOn = savedInstanceState.getBoolean(STATE_KEY_LOCATION_MODE);
            mapMode = savedInstanceState.getBoolean(STATE_KEY_MAP_MODE);

            setMyLocationState(btnEnablePositioning, isLocationOn);
            setMapModeState(btnMapMode, mapMode);
        }
    }
}
